package study_HttpClient;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpClient的Get方法带参数 {
    public static void main(String[] args) throws Exception{
        //1.创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //设置请求地址是：https://search.bilibili.com/all?keyword=java
        //创建URIBuilder
        URIBuilder uriBuilder = new URIBuilder("https://search.bilibili.com/all");
        uriBuilder.setParameter("keyword","java");

        //2.创建HttpGet对象,设置参数
        HttpGet httpGet = new HttpGet(uriBuilder.build());

        CloseableHttpResponse response = null;

        //3.使用HttpClient发起请求，获取response
        response = httpClient.execute(httpGet);

        //4.解析响应
        if (response.getStatusLine().getStatusCode() == 200){
            String content = EntityUtils.toString(response.getEntity(),"utf8");
            System.out.println(content.length());
        }

        //5.关闭response
        response.close();
        //6.关闭httpClient
        httpClient.close();
    }
}
